#include "mainwindow.h"
#include "titlebar.h"
#include <QApplication>
#include <QMainWindow>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QTranslator>
#include <QCoreApplication>
#include <QPixmap>
#include <QDebug>
#include <QMessageBox>
#include <QLibraryInfo>
#include <unistd.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/file.h>
#include <fcntl.h>
#include <syslog.h>
#include <ukui-log4qt.h>
#include <KWindowEffects>
#include "customstyle.h"
#include "util/xatom-helper.h"
#define TRANS_PATH  "/usr/share/translations/"

int main(int argc, char *argv[])
{
    initUkuiLog4qt("kylin-camera");//日志功能,穿版本之前务必解开
    printf("Program start ...\n");
    // 适配4K屏
    #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    #endif

    #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
        QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    #endif

    QApplication a(argc, argv);
    a.setWindowIcon(QIcon::fromTheme("kylin-camera"));

    QString trans_path;
    if (QDir(TRANS_PATH).exists()) {
        trans_path = TRANS_PATH;
    }
    else {
        trans_path = qApp->applicationDirPath() + "/translations";
    }
    QTranslator app_trans;
    QString locale = QLocale::system().name();
    if (locale == "zh_CN") {
        if(app_trans.load("kylin-camera_" + locale + ".qm", trans_path))
           qDebug()<<"加载翻译文件："<< a.installTranslator(&app_trans);
    }
    QString translatorFileName = QLatin1String("qt_");
    translatorFileName += QLocale::system().name();
    QTranslator *translator = new QTranslator();
    if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    {
        a.installTranslator(translator);
    }
    else
    {
        qDebug() << "加载中文失败";
    }

    QStringList strList;
    for(int i = 0; argv[i];i++){
        if(argv[i]){
            QString a = argv[i];
            strList.push_back(a);
            qDebug() << i + ":" + a;
        }
    }

//    QString str = "";
//    if(strList.size() > 1)   //strList[0] == "kylin-camera"
//    {
//        str = strList[1];
//    }
//    if(str != "")
//    {
//       if(str == "--help" || str == "--h")//帮助
//       {
//           qDebug()<<"\nkylin-camera [cmd]\n"
//                     "-c -cheese  拍照\n"
//                     "-v -video   录像\n"
//                     "-s -stop    结束录像\n"
//                     "-cm -cmode  切换拍照模式\n"
//                     "-vm -vmode  切换录像模式\n"
//                     "-ssd -sdirectory [save directory]保存目录\n"
//                     "-q -quit 退出";
//           return 0;
//       }
//       //如果参数不是命令也不是文件路径，则退出
//       if(!QFileInfo::exists(str) && str != "-c" && str != "-cheese" && str != "-v" && str!="-video" && str!="-s" &&
//               str != "-stop" && str != "-ssd" && str != "-q" && str != "-quit" && str != "-cm" && str != "-cmode" &&
//               str != "-vm" && str != "-vmode" && str != "-ssd" && str != "-sdirectory" && str != "-a" &&
//               str != "album" && str != "-d" && str != "-delay" && str != "-m" && str != "-mirror")
//       {
//           qDebug()<<"参数不合规，请使用kylin-camera --help参数获取帮助";
//           return 0;
//       }
//    }

    MainWindow w(strList);

#ifndef __V10__
    //添加窗管协议
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(w.winId(), hints);
#endif
    w.show();
    KWindowEffects::enableBlurBehind(w.winId(),true);
    return a.exec();
}
